package com.ruoyi.qywx.getway.entity;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.cache.Meta;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.StrUtils;
import com.ruoyi.qywx.entity.QyWelcomeTemplate;
import com.ruoyi.qywx.enums.QyMessageEm;
import com.ruoyi.qywx.enums.QywxMediaEm;
import com.ruoyi.qywx.getway.api.QyMediaUtil;
import com.ruoyi.qywx.getway.keys.QywxKey;

import java.io.Serializable;
import java.util.*;

/**
 * @author : hcl
 * @Date : 2023/8/21 15:07
 */
public class QyWelcome extends QywxKey implements Serializable {
    private static final long serialVersionUID = 1362681128499440094L;
    //    欢迎语
    private final Map<String, Object> welcome;
    //    绑定标签
    private String bindTags;
    //    添加备注
    private String remark;

    private QyWelcome() {
        this.welcome = new HashMap<>();
    }

    public static QyWelcome of(QyWelcomeTemplate welcomeTemp,
                               String tags,
                               String remark,
                               Meta meta) {
        if (Objects.isNull(welcomeTemp)) return null;
        QyWelcome welcome = new QyWelcome();
        welcome.text(welcomeTemp.getText());
        JSONArray others = JSONUtil.parseArray(welcomeTemp.getOthers());
        welcome.findAttachements(others, meta);
        welcome.bindTags = tags;
        welcome.remark = remark;
        return welcome;
    }

    private void findAttachements(JSONArray others, Meta meta) {
        List<Map<String, Object>> attachments = new ArrayList<>();
        others.forEach(e -> {
            JSONObject content = (JSONObject) e;
            QyMessageEm mediaType = QyMessageEm.valueOf(content.getStr(TYPE).toUpperCase());
            switch (mediaType) {
                case TEXT:
                    break;
                case PIC:
                    CollectionUtils.add(attachments, this.image(content.getJSONObject(CONTENT), meta));
                    break;
                case VIDEO:
                    CollectionUtils.add(attachments, this.file(content.getJSONObject(CONTENT), QywxMediaEm.VIDEO.getType(), meta));
                    break;
                case LINK:
                    CollectionUtils.add(attachments, this.link(content.getJSONObject(CONTENT)));
                case MINI:
                    CollectionUtils.add(attachments, this.miniprogram(content.getJSONObject(CONTENT), meta));
//                case VOICE:
//                    CollectionUtils.add(attachments, welcome.file(content.getJSONObject(mediaKey), "file", meta));
                default:
                    CollectionUtils.add(attachments, this.file(content.getJSONObject(CONTENT), QywxMediaEm.FILE.getType(), meta));
            }
        });
        this.welcome.put(ATTACHEMENTS, attachments);
    }

    /**
     * 小程序
     */
    private Map<String, Object> miniprogram(JSONObject json, Meta meta) {
        String mediaId = QyMediaUtil.getImageMediaId(meta, json.getStr(URL));
        if (StrUtils.isEmpty(mediaId)) {
            return null;
        }
        Map<String, String> miniprogram = new HashMap<>();
        miniprogram.put(TITLE, json.getStr(TITLE));
        miniprogram.put(PIC_MEDIA_ID, mediaId);
        miniprogram.put(APPID, json.getStr("appId"));
        miniprogram.put(PAGE, json.getStr(PAGE));

        Map<String, Object> miniMsg = new HashMap<>();
        String mimi = QyMessageEm.MINI.getName();
        miniMsg.put(mimi, miniprogram);
        miniMsg.put(MSG_TYPE, mimi);
        return miniMsg;
    }

    private Map<String, Object> link(JSONObject json) {
        String name = QyMessageEm.LINK.getName();
        Map<String, String> link = new HashMap<>();
        link.put(TITLE, json.getStr(TITLE));
        link.put(PIC_URL, json.getStr(PIC_URL));
        link.put(DESC, json.getStr(DESC));
        link.put(URL, json.getStr(URL));

        Map<String, Object> linkMsg = new HashMap<>();
        linkMsg.put(MSG_TYPE, name);
        linkMsg.put(name, link);
        return linkMsg;
    }

    private Map<String, Object> file(JSONObject json, String fileType, Meta meta) {
        String mediaId = QyMediaUtil.getFileMediaId(meta,
                json.getStr(NAME),
                json.getStr(URL));
        if (StrUtils.isEmpty(mediaId)) return null;
        Map<String, Object> file = new HashMap<>();
        file.put(MSG_TYPE, fileType);
        file.put(fileType, Collections.singletonMap(MEDIA_ID, mediaId));
        return file;
    }

    private Map<String, Object> image(JSONObject json, Meta meta) {
        String mediaId = QyMediaUtil.getImageMediaId(meta, json.getStr(URL));
        if (StrUtils.isEmpty(mediaId)) {
            return null;
        }
        String imageStr = QywxMediaEm.IMAGE.getType();
        Map<String, Object> image = new HashMap<>();
        image.put(MSG_TYPE, imageStr);
        image.put(imageStr, Collections.singletonMap(MEDIA_ID, mediaId));
        return image;
    }

    private void text(String text) {
        welcome.put(TEXT, Collections.singletonMap(CONTENT, text));
    }

    public Map<String, Object> getWelcome() {
        return welcome;
    }

    public String getBindTags() {
        return bindTags;
    }

    public String getRemark() {
        return remark;
    }
}
